﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.ZCWX;
using StateGrid.User;
using StateGrid.GlobalVariables;
using iTextSharp.text.pdf;
using StateGrid.OperationLog;

public partial class AddPM : System.Web.UI.Page
{
    /// <summary>
    /// 中文期刊操作对象
    /// </summary>
    private ZCWX_DAO zcwxDAO = new ZCWX_DAO();
    /// <summary>
    /// 日志操作对象
    /// </summary>
    private OperationLog_DAO operationLogDAO = new OperationLog_DAO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }

        if (!this.IsPostBack)
        {
            this.BindData();
        }

    }

    /// <summary>
    /// 绑定初始化数据
    /// </summary>
    private void BindData()
    {
        
        String message = null;
        String sybm = HttpUtility.UrlDecode(Request["sybm"]);
        if (null == sybm || "".Equals(sybm))
        {
            message = "错误：参数错误";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }
        
        //指定索引的自产文献
        ZCWX_PM zcwxPM = this.zcwxDAO.GetZCWX_PMBySybm(sybm);

        if (null == zcwxPM)
        {
            message = "错误：没有找到该自产文献";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }

        User user = (User)Session["login"];
        //登陆超时则退到登录页
        if (null == user)
        {
            Response.Write(" <script> top.location = '" + GlobalResource.LOGIN_PAGE + "' </script> ");
            return;
        }

        this.backHyperLink.NavigateUrl = GlobalZCWX.LISTPM_PAGE + "?catalog=" + zcwxPM.flbm;

        ZCWX_FL zcwxFL = zcwxDAO.GetZCWX_FLByFlbm(zcwxPM.flbm); 

        //分类编码
        this.flbmTextBox.Text = zcwxPM.flbm;
        //分类名称
        this.flmcTextBox.Text = zcwxFL.flmc;
        //单位代码
        this.dwdmTextBox.Text = zcwxFL.dwdm;
        //年份
        this.cbnfTextBox.Text = zcwxPM.sybm.Split('-')[1];
        //出版单位
        this.cbdwTextBox.Text = zcwxPM.cbdw;

        this.gchTextBox.Text = zcwxPM.gch;//馆藏号
        this.zwpmTextBox.Text = zcwxPM.zwpm;//中文篇名
        this.zwztcTextBox.Text = zcwxPM.zwztc;//中文主题词
        this.wxmjTextBox.Text = zcwxPM.wxmj;//文献密级
        this.wxlxTextBox.Text = zcwxPM.wxlx; //文献类型

        this.zrgrTextBox.Text = zcwxPM.zrgr; //责任个人
        this.zrdwTextBox.Text = zcwxPM.zrdw; //责任单位
        this.ztxtTextbox.Text = zcwxPM.ztxt; //载体形态
        this.gtflTextBox.Text = zcwxPM.gtfl; //国图分类
        this.zwwzTextBox.Text = zcwxPM.zwwz;//中文文摘

        if (null != zcwxPM.cbrq)
        {//出版日期
            DateTime dt = (DateTime)zcwxPM.cbrq;
            this.cbrqTextBox.Text = dt.ToString("yyyy-MM-dd");
        }
        this.bzTextBox.Text = zcwxPM.bz; //备注

        this.sybmTextBox.Text = zcwxPM.sybm;//索引编码

        this.SetPreAndNextPM();
    }

    /// <summary>
    /// 获取用户填写的自产文献篇名信息
    /// </summary>
    /// <returns></returns>
    private ZCWX_PM GetZCWXPM()
    {
        ZCWX_PM zcwxPM = new ZCWX_PM();
        //索引编码
        zcwxPM.sybm = this.sybmTextBox.Text;
        //分类编码
        zcwxPM.flbm = this.flbmTextBox.Text;
        //出版单位
        zcwxPM.cbdw = this.cbdwTextBox.Text;
        //馆藏号
        zcwxPM.gch = this.gchTextBox.Text.Trim();
        //中文篇名
        zcwxPM.zwpm = this.zwpmTextBox.Text.Trim();
        //主题词
        zcwxPM.zwztc = this.zwztcTextBox.Text.Trim();
        //文献密级
        zcwxPM.wxmj = this.wxmjTextBox.Text.Trim();
        //文献类型
        zcwxPM.wxlx = this.wxlxTextBox.Text.Trim();
        //出版日期
        if (!"".Equals(this.cbrqTextBox.Text))
            zcwxPM.cbrq = DateTime.Parse(this.cbrqTextBox.Text);
        //载体形态
        zcwxPM.ztxt = this.ztxtTextbox.Text.Trim();
        //责任个人
        zcwxPM.zrgr = this.zrgrTextBox.Text.Trim();
        //责任单位
        zcwxPM.zrdw = this.zrdwTextBox.Text.Trim();
        //国图分类
        zcwxPM.gtfl = this.gtflTextBox.Text.Trim();
        //中文文摘
        zcwxPM.zwwz = this.zwwzTextBox.Text.Trim();
        //备注
        zcwxPM.bz = this.bzTextBox.Text.Trim();
        //更新日期
        zcwxPM.gxrq = DateTime.Now;

        return zcwxPM;
    }

    /// <summary>
    /// 获取下一个索引编码
    /// </summary>
    /// <param name="sybm"></param>
    /// <returns></returns>
    private String GetSybm(String sybm)
    {
        String result = null;
        if (null == sybm || "".Equals(sybm))
        {
            result = this.dwdmTextBox.Text + "-" + this.cbnfTextBox.Text + "-0001";
        }
        else
        {
            //获取文件名的后四位流水号，并在其基础上加1
            String[] str = sybm.Split('-');

            String phString = str[2];

            int ph = 0;

            try
            {
                ph = Int32.Parse(phString);
            }
            catch (Exception)
            {
            }

            //当前篇号+1
            ph += 1;

            phString = ph.ToString();
            //长度为1
            if (phString.Length == 1)
            {
                phString = "000" + phString;
            }
            else if (phString.Length == 2)
            {
                phString = "00" + phString;
            }
            else if (phString.Length == 3)
            {
                phString = "0" + phString;
            }
            else if (phString.Length >= 3)
            {
                String message = "错误：篇名数量已经超过了1000篇";
                Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
                return null;
            }

            result = this.dwdmTextBox.Text + "-" + this.cbnfTextBox.Text + "-" + phString;
        }
        return result;
    }

    protected void submitButton_Click(object sender, EventArgs e)
    {
        //验证中文篇名
        if (!this.ValidateZWPM())
        {
            return;
        }
        //验证出版日期
        if (!this.ValidateCBRQ())
        {
            return;
        }

        ZCWX_PM zcwxPM = this.GetZCWXPM();
        if (this.zcwxDAO.UpdateZCWX_PM(zcwxPM, ((User)Session["login"]).name))
        {
            Response.Write("<Script>alert('修改自产文献信息成功')</Script>");
            this.NextPM();
        }
        else
        {
            Response.Write("<Script>alert('修改自产文献信息失败')</Script>");
        }
    }

    /// <summary>验证出版日期</summary>
    private Boolean ValidateCBRQ()
    {
        this.errorLabel.Text = "";

        Boolean flag = true;
        if (this.cbrqTextBox != null && !"".Equals(this.cbrqTextBox.Text))
        {
            try
            {
                DateTime val = DateTime.Parse(this.cbrqTextBox.Text);
            }
            catch (Exception)
            {
                this.errorLabel.Text = "错误：出版日期不正确(格式YYYY-MM-DD）";
                return false;
            }

            return true;
        }

        return flag;
    }

    /// <summary>
    /// 验证中文篇名
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateZWPM()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        if(null==this.zwpmTextBox.Text||"".Equals(this.zwpmTextBox.Text.Trim()))
        {
            this.errorLabel.Text = "错误：中文篇名不能为空";
            flag = false;
        }
        return flag;
    }

    protected void resetButton_Click(object sender, EventArgs e)
    {
        this.BindData();
    }
    //全角转半角
    protected void zrgrTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zrgrTextBox.Text = GlobalMethod.Semicolon(this.zrgrTextBox.Text.Trim());
    }
    protected void zwztcTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zwztcTextBox.Text = GlobalMethod.Semicolon(this.zwztcTextBox.Text.Trim());
    }
    protected void gtflTextBox_TextChanged(object sender, EventArgs e)
    {
        this.gtflTextBox.Text = GlobalMethod.Semicolon(this.gtflTextBox.Text.Trim());
    }
    /// <summary>
    /// 设置前一篇和后一篇的链接
    /// </summary>
    private void SetPreAndNextPM()
    {
        //上一篇
        String pre = this.GetDirectionPM(-1);
        if (null != pre)
        {
            this.preHyperLink.NavigateUrl = GlobalZCWX.EDITPM_PAGE + "?sybm=" + pre;
        }

        //下一篇
        String next = this.GetDirectionPM(1);
        if (null != next)
        {
            this.nextHyperLink.NavigateUrl = GlobalZCWX.EDITPM_PAGE + "?sybm=" + next;
        }
    }
    /// <summary>
    /// 修改下一篇
    /// </summary>
    private void NextPM()
    {
        String sybm = this.GetDirectionPM(1);
        if (null == sybm)
        {
            return;
        }
        else
        {
            Response.Redirect(GlobalZCWX.EDITPM_PAGE + "?sybm=" + sybm);
        }
    }

    /// <summary>
    /// 获取下一个文件名，不包括后缀
    /// </summary>
    /// <param name="wjm"></param>
    /// <param name="direction">大于0下一篇；反之，上一篇</param>
    /// <returns></returns>
    private String GetDirectionPM(int direction)
    {
        String result = null;
        //获取文件名的后四位流水号，并在其基础上加1

        String phString = this.sybmTextBox.Text.Split('-')[2];

        int ph = 0;

        try
        {
            ph = Int32.Parse(phString);
        }
        catch (Exception)
        {
        }

        //当前篇号+1或-1
        if (direction > 0)
        {
            ph++;
        }
        else
        {
            ph--;
        }

        phString = ph.ToString();
        //长度为1
        if (phString.Length == 1)
        {
            phString = "000" + phString;
        }
        else if (phString.Length == 2)
        {
            phString = "00" + phString;
        }
        else if (phString.Length == 3)
        {
            phString = "0" + phString;
        }

        result = this.dwdmTextBox.Text + "-" + this.cbnfTextBox.Text + "-" + phString;
        ZCWX_PM ZCWXPM = this.zcwxDAO.GetZCWX_PMBySybm(result);
        if (null == ZCWXPM)
        {
            return null;
        }
        return result;
    }
}